/**
 * @file MainWindow.cpp
 * @brief 主窗口类实现
 * @details 实现了应用程序的主窗口界面功能
 */

#include "MainWindow.h"
#include <Application.h>
#include <LayoutBuilder.h>
#include <Menu.h>
#include <MenuItem.h>
#include <Alert.h>
#include <PopUpMenu.h>
#include <MenuField.h>
#include <OptionPopUp.h>
#include <Box.h>
#include <SpaceLayoutItem.h>
#include <RadioButton.h>
#include <CheckBox.h>
#include <TextControl.h>
#include <TabView.h>
#include <ListView.h>
#include <ScrollView.h>
#include <ColumnListView.h>
#include <ColumnTypes.h>
#include <Font.h>
#include <Path.h>
#include <FindDirectory.h>
#include <stdio.h>
#include <stdlib.h>

/**
 * @brief 构造函数
 * @param frame 窗口位置和大小
 */
MainWindow::MainWindow(BRect frame)
    : BWindow(frame, "HZip - 压缩/解压缩工具", B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS | B_AUTO_UPDATE_SIZE_LIMITS),
      fOpenPanel(NULL),
      fSavePanel(NULL),
      fCurrentFormat("zip"),
      fSpeedRadioButton(NULL),
      fSizeRadioButton(NULL),
      fCustomRadioButton(NULL),
      fPasswordCheckBox(NULL),
      fPasswordTextControl(NULL),
      fSmartTipView(NULL),
      fConfigBox(NULL),
      fExtractConfigBox(NULL),
      fTargetDirView(NULL),
      fSmartDeleteCheckBox(NULL),
      fKeepBrokenCheckBox(NULL),
      fNoSubdirCheckBox(NULL),
      fExtractLaterCheckBox(NULL),
      fSaveSettingsButton(NULL),
      fExtractNowButton(NULL),
      fExtractDirPanel(NULL),
      fTabView(NULL),
      fHomeView(NULL),
      fCompressView(NULL),
      fExtractView(NULL),
      fFileListView(NULL),
      fAddButton(NULL),
      fRemoveButton(NULL),
      fStartButton(NULL),
      fIconFont(NULL)
{
    // 初始化界面
    _InitInterface();

    // 创建文件选择面板
    fOpenPanel = new BFilePanel(B_OPEN_PANEL, NULL, NULL, B_FILE_NODE | B_DIRECTORY_NODE, false, new BMessage(MSG_FILE_PANEL_OPEN));
    fSavePanel = new BFilePanel(B_SAVE_PANEL, NULL, NULL, B_FILE_NODE, false, new BMessage(MSG_FILE_PANEL_SAVE));
}

/**
 * @brief 析构函数
 */
MainWindow::~MainWindow()
{
    // 释放资源
    delete fOpenPanel;
    delete fSavePanel;
    delete fExtractDirPanel;
    delete fIconFont;
}

/**
 * @brief 消息处理函数
 * @param message 收到的消息
 */
void MainWindow::MessageReceived(BMessage *message)
{
    switch (message->what)
    {
    case MSG_TAB_CHANGED:
        // 处理标签页切换
        break;

    case MSG_ADD_FILES:
        // 添加文件到列表
        fOpenPanel->Show();
        break;

    case MSG_REMOVE_FILES:
        // 从列表中删除选中的文件
        if (fFileListView)
        {
            int32 selection = fFileListView->CurrentSelection();
            if (selection >= 0)
            {
                fFileListView->RemoveItem(selection);
            }
        }
        break;

    case MSG_START_OPERATION:
        // 开始压缩或解压缩操作
        if (fTabView)
        {
            int32 selection = fTabView->Selection();
            if (selection == 1)
            { // 压缩标签页
                _DoCompress();
            }
            else if (selection == 2)
            { // 解压标签页
                _DoExtract();
            }
        }
        break;
    case MSG_MENU_FILE_OPEN:
        // 打开文件选择面板
        fOpenPanel->Show();
        break;

    case MSG_MENU_FILE_SAVE:
        // 打开保存文件面板
        fSavePanel->Show();
        break;

    case MSG_MENU_FILE_QUIT:
        // 退出应用程序
        be_app->PostMessage(B_QUIT_REQUESTED);
        break;

    case MSG_MENU_HELP_ABOUT:
        // 显示关于对话框
        _ShowAbout();
        break;

    case MSG_BTN_COMPRESS:
        // 执行压缩操作
        _DoCompress();
        break;

    case MSG_BTN_EXTRACT:
        // 执行解压缩操作
        _DoExtract();
        break;

    case MSG_EXTRACT_DIR_SELECTED:
        // 处理解压目录选择
        {
            entry_ref ref;
            if (message->FindRef("refs", &ref) == B_OK)
            {
                BEntry entry(&ref);
                BPath path;
                if (entry.GetPath(&path) == B_OK && entry.IsDirectory())
                {
                    fTargetPath.SetTo(path.Path());
                    if (fTargetDirView)
                    {
                        fTargetDirView->SetText(path.Path());
                    }
                }
            }
        }
        break;

    case MSG_EXTRACT_OPTION_TOGGLE:
        // 处理解压选项切换
        // 这里可以根据需要添加特定选项的处理逻辑
        break;

    case MSG_EXTRACT_SAVE_SETTINGS:
        // 保存解压设置
        {
            BAlert *alert = new BAlert("提示", "设置已保存", "确定");
            alert->Go();
        }
        break;

    case MSG_FORMAT_SELECTED:
        // 更新当前选择的压缩格式
        {
            const char *format;
            if (message->FindString("format", &format) == B_OK)
            {
                fCurrentFormat = format;
            }
        }
        break;

    case MSG_COMPRESS_SPEED:
        // 选择速度最快压缩选项
        if (fSpeedRadioButton)
            fSpeedRadioButton->SetValue(B_CONTROL_ON);
        if (fSizeRadioButton)
            fSizeRadioButton->SetValue(B_CONTROL_OFF);
        if (fCustomRadioButton)
            fCustomRadioButton->SetValue(B_CONTROL_OFF);
        if (fSmartTipView)
            fSmartTipView->SetText("智能为图片、视频、文档等批量优化压缩方式，兼具速度和压缩率");
        break;

    case MSG_COMPRESS_SIZE:
        // 选择体积最小压缩选项
        if (fSpeedRadioButton)
            fSpeedRadioButton->SetValue(B_CONTROL_OFF);
        if (fSizeRadioButton)
            fSizeRadioButton->SetValue(B_CONTROL_ON);
        if (fCustomRadioButton)
            fCustomRadioButton->SetValue(B_CONTROL_OFF);
        if (fSmartTipView)
            fSmartTipView->SetText("智能为图片、视频、文档等批量优化压缩方式，兼具速度和压缩率");
        break;

    case MSG_COMPRESS_CUSTOM:
        // 选择自定义压缩选项
        if (fSpeedRadioButton)
            fSpeedRadioButton->SetValue(B_CONTROL_OFF);
        if (fSizeRadioButton)
            fSizeRadioButton->SetValue(B_CONTROL_OFF);
        if (fCustomRadioButton)
            fCustomRadioButton->SetValue(B_CONTROL_ON);
        if (fSmartTipView)
            fSmartTipView->SetText("自定义压缩选项，可以根据需要调整压缩参数");
        break;

    case MSG_PASSWORD_TOGGLE:
        // 密码选项开关
        if (fPasswordCheckBox && fPasswordTextControl)
        {
            bool isChecked = (fPasswordCheckBox->Value() == B_CONTROL_ON);
            fPasswordTextControl->SetEnabled(isChecked);
        }
        break;

    case MSG_FILE_PANEL_OPEN:
        // 处理文件选择面板的结果
        {
            entry_ref ref;
            if (message->FindRef("refs", &ref) == B_OK)
            {
                BEntry entry(&ref);
                BPath path;
                if (entry.GetPath(&path) == B_OK)
                {
                    fSourcePath.SetTo(path.Path());
                    fStatusView->SetText(path.Path());
                }
            }
        }
        break;

    case MSG_FILE_PANEL_SAVE:
        // 处理保存文件面板的结果
        {
            entry_ref ref;
            const char *name;
            if (message->FindRef("directory", &ref) == B_OK && message->FindString("name", &name) == B_OK)
            {
                BPath path(&ref);
                path.Append(name);
                fTargetPath.SetTo(path.Path());

                // 如果是压缩操作，确保文件有正确的扩展名
                BString pathStr(path.Path());
                if (fCurrentFormat == "zip" && !pathStr.IEndsWith(".zip"))
                {
                    fTargetPath.Append(".zip");
                }
                else if (fCurrentFormat == "tar" && !pathStr.IEndsWith(".tar"))
                {
                    fTargetPath.Append(".tar");
                }
                else if (fCurrentFormat == "tar.gz" && !pathStr.IEndsWith(".tar.gz") && !pathStr.IEndsWith(".tgz"))
                {
                    fTargetPath.Append(".tar.gz");
                }
                else if (fCurrentFormat == "tar.bz2" && !pathStr.IEndsWith(".tar.bz2") && !pathStr.IEndsWith(".tbz2"))
                {
                    fTargetPath.Append(".tar.bz2");
                }
                else if (fCurrentFormat == "gz" && !pathStr.IEndsWith(".gz"))
                {
                    fTargetPath.Append(".gz");
                }
                else if (fCurrentFormat == "bz2" && !pathStr.IEndsWith(".bz2"))
                {
                    fTargetPath.Append(".bz2");
                }

                // 执行压缩操作
                _DoCompress();
            }
        }
        break;

    default:
        BWindow::MessageReceived(message);
        break;
    }
}

/**
 * @brief 窗口退出处理函数
 */
bool MainWindow::QuitRequested()
{
    be_app->PostMessage(B_QUIT_REQUESTED);
    return true;
}

/**
 * @brief 初始化界面
 */
void MainWindow::_InitInterface()
{
    // 加载图标字体
    fIconFont = new BFont();
    fIconFont->SetFamilyAndStyle("FontAwesome", "Solid");
    fIconFont->SetSize(16.0);

    // 创建主视图
    fMainView = new BView("MainView", B_WILL_DRAW);
    fMainView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));

    // 创建菜单栏
    BMenuBar *menuBar = _CreateMenuBar();

    // 创建格式选择菜单
    BPopUpMenu *formatMenu = new BPopUpMenu("格式");
    formatMenu->AddItem(new BMenuItem("ZIP (.zip)", new BMessage(MSG_FORMAT_SELECTED)));
    formatMenu->AddItem(new BMenuItem("TAR (.tar)", new BMessage(MSG_FORMAT_SELECTED)));
    formatMenu->AddItem(new BMenuItem("TAR.GZ (.tar.gz)", new BMessage(MSG_FORMAT_SELECTED)));
    formatMenu->AddItem(new BMenuItem("TAR.BZ2 (.tar.bz2)", new BMessage(MSG_FORMAT_SELECTED)));
    formatMenu->AddItem(new BMenuItem("GZIP (.gz)", new BMessage(MSG_FORMAT_SELECTED)));
    formatMenu->AddItem(new BMenuItem("BZIP2 (.bz2)", new BMessage(MSG_FORMAT_SELECTED)));
    formatMenu->ItemAt(0)->SetMarked(true);

    BMenuField *formatField = new BMenuField("formatField", "压缩格式: ", formatMenu);

    // 创建状态文本视图
    fStatusView = new BStringView("statusView", "请选择文件或目录");

    // 创建压缩配置选项
    fConfigBox = new BBox("configBox");
    fConfigBox->SetLabel("压缩配置");

    // 创建压缩方式选项
    fSpeedRadioButton = new BRadioButton("speedRadio", "速度最快", new BMessage(MSG_COMPRESS_SPEED));
    fSizeRadioButton = new BRadioButton("sizeRadio", "体积最小", new BMessage(MSG_COMPRESS_SIZE));
    fCustomRadioButton = new BRadioButton("customRadio", "自定义", new BMessage(MSG_COMPRESS_CUSTOM));

    // 默认选择速度最快选项
    fSpeedRadioButton->SetValue(B_CONTROL_ON);

    // 创建智能压缩提示
    fSmartTipView = new BStringView("smartTipView", "智能为图片、视频、文档等批量优化压缩方式，兼具速度和压缩率");
    fSmartTipView->SetFont(be_plain_font);

    // 创建密码选项
    fPasswordCheckBox = new BCheckBox("passwordCheck", "添加密码", new BMessage(MSG_PASSWORD_TOGGLE));
    fPasswordTextControl = new BTextControl("passwordText", "", "", NULL);
    fPasswordTextControl->TextView()->HideTyping(true); // 密码输入框隐藏输入内容
    fPasswordTextControl->SetEnabled(false);            // 默认禁用密码输入框

    // 创建按钮
    fCompressButton = new BButton("compressButton", "立即压缩", new BMessage(MSG_BTN_COMPRESS));
    fExtractButton = new BButton("extractButton", "解压缩", new BMessage(MSG_BTN_EXTRACT));

    // 组织压缩配置界面
    BView *configView = new BView("configView", B_WILL_DRAW);
    configView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));

    BLayoutBuilder::Group<>(configView, B_VERTICAL, 10)
        .SetInsets(10, 15, 10, 10)
        .AddGroup(B_HORIZONTAL, 20)
        .Add(fSpeedRadioButton)
        .Add(fSizeRadioButton)
        .Add(fCustomRadioButton)
        .AddGlue()
        .End()
        .Add(fSmartTipView)
        .AddStrut(5)
        .AddGroup(B_HORIZONTAL, 10)
        .Add(fPasswordCheckBox)
        .Add(fPasswordTextControl)
        .AddGlue()
        .End();

    fConfigBox->AddChild(configView);

    // 创建解压配置界面
    fExtractConfigBox = new BBox("extractConfigBox");
    fExtractConfigBox->SetLabel("解压配置");

    // 创建目标路径显示
    fTargetDirView = new BStringView("targetDirView", "目标路径：(如果不存在将会创建)");

    // 创建解压选项
    fSmartDeleteCheckBox = new BCheckBox("smartDeleteCheck", "智能删除多余的文件类", new BMessage(MSG_EXTRACT_OPTION_TOGGLE));
    fKeepBrokenCheckBox = new BCheckBox("keepBrokenCheck", "保留损坏文件", new BMessage(MSG_EXTRACT_OPTION_TOGGLE));
    fNoSubdirCheckBox = new BCheckBox("noSubdirCheck", "不解压到子目录", new BMessage(MSG_EXTRACT_OPTION_TOGGLE));
    fExtractLaterCheckBox = new BCheckBox("extractLaterCheck", "后台解压", new BMessage(MSG_EXTRACT_OPTION_TOGGLE));

    // 创建解压按钮
    fSaveSettingsButton = new BButton("saveSettingsButton", "保存设置", new BMessage(MSG_EXTRACT_SAVE_SETTINGS));
    fExtractNowButton = new BButton("extractNowButton", "立即解压", new BMessage(MSG_BTN_EXTRACT));

    // 组织解压配置界面
    BView *extractConfigView = new BView("extractConfigView", B_WILL_DRAW);
    extractConfigView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));

    BLayoutBuilder::Group<>(extractConfigView, B_VERTICAL, 10)
        .SetInsets(10, 15, 10, 10)
        .Add(fTargetDirView)
        .AddStrut(5)
        .AddGroup(B_HORIZONTAL, 20)
        .Add(fSmartDeleteCheckBox)
        .Add(fKeepBrokenCheckBox)
        .AddGlue()
        .End()
        .AddGroup(B_HORIZONTAL, 20)
        .Add(fNoSubdirCheckBox)
        .Add(fExtractLaterCheckBox)
        .AddGlue()
        .End()
        .AddStrut(5)
        .AddGroup(B_HORIZONTAL, 10)
        .AddGlue()
        .Add(fSaveSettingsButton)
        .Add(fExtractNowButton)
        .End();

    fExtractConfigBox->AddChild(extractConfigView);

    // 默认隐藏解压配置界面
    fExtractConfigBox->Hide();

    // 创建解压目录选择面板
    fExtractDirPanel = new BFilePanel(B_OPEN_PANEL, NULL, NULL, B_DIRECTORY_NODE, false,
                                      new BMessage(MSG_EXTRACT_DIR_SELECTED), NULL, false, true);
    fExtractDirPanel->SetButtonLabel(B_DEFAULT_BUTTON, "选择");
    fExtractDirPanel->Window()->SetTitle("选择解压目标目录");

    // 创建标签页视图
    fTabView = new BTabView("tabView", B_WIDTH_FROM_WIDEST);

    // 创建主页视图
    fHomeView = new BView("homeView", B_WILL_DRAW);
    fHomeView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));

    // 创建压缩视图
    fCompressView = new BView("compressView", B_WILL_DRAW);
    fCompressView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));

    // 创建解压视图
    fExtractView = new BView("extractView", B_WILL_DRAW);
    fExtractView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));

    // 创建文件列表视图
    fFileListView = new BColumnListView("fileListView", B_NAVIGABLE, B_FANCY_BORDER);
    fFileListView->AddColumn(new BStringColumn("文件名", 180, 50, 500, B_TRUNCATE_END), 0);
    fFileListView->AddColumn(new BStringColumn("大小", 80, 50, 150, B_TRUNCATE_END), 1);
    fFileListView->AddColumn(new BStringColumn("类型", 80, 50, 150, B_TRUNCATE_END), 2);
    fFileListView->AddColumn(new BStringColumn("修改日期", 120, 50, 200, B_TRUNCATE_END), 3);
    fFileListView->SetSelectionMode(B_SINGLE_SELECTION_LIST);

    // 创建操作按钮
    BString addIconStr;
    addIconStr.Append((char *)&(uint32)0xF067, 4); // 加号图标
    fAddButton = new BButton("addButton", "添加文件", new BMessage(MSG_ADD_FILES));

    BString removeIconStr;
    removeIconStr.Append((char *)&(uint32)0xF068, 4); // 减号图标
    fRemoveButton = new BButton("removeButton", "删除文件", new BMessage(MSG_REMOVE_FILES));

    BString startIconStr;
    startIconStr.Append((char *)&(uint32)0xF04B, 4); // 播放图标
    fStartButton = new BButton("startButton", "开始操作", new BMessage(MSG_START_OPERATION));

    // 组织主页视图
    BStringView *welcomeView = new BStringView("welcomeView", "欢迎使用HZip压缩/解压缩工具");
    welcomeView->SetFont(be_bold_font);

    BStringView *iconView = new BStringView("iconView", "");
    BString archiveIconStr;
    archiveIconStr.Append((char *)&(uint32)0xF1C6, 4); // 压缩文件图标
    iconView->SetFont(fIconFont);
    iconView->SetText(archiveIconStr);
    iconView->SetFontSize(48.0);

    BStringView *descView = new BStringView("descView", "HZip是一个基于Haiku OS原生API开发的压缩/解压缩软件，支持多种常见压缩文件格式。");

    BLayoutBuilder::Group<>(fHomeView, B_VERTICAL, 20)
        .SetInsets(20, 20, 20, 20)
        .Add(welcomeView)
        .Add(iconView)
        .Add(descView)
        .AddGroup(B_HORIZONTAL, 10)
        .AddGlue()
        .Add(fCompressButton)
        .Add(fExtractButton)
        .AddGlue()
        .End()
        .AddGlue();

    // 组织压缩视图
    BLayoutBuilder::Group<>(fCompressView, B_VERTICAL, 10)
        .SetInsets(10, 10, 10, 10)
        .Add(formatField)
        .Add(fFileListView)
        .AddGroup(B_HORIZONTAL, 10)
        .Add(fAddButton)
        .Add(fRemoveButton)
        .AddGlue()
        .Add(fStartButton)
        .End()
        .Add(fConfigBox);

    // 组织解压视图
    BLayoutBuilder::Group<>(fExtractView, B_VERTICAL, 10)
        .SetInsets(10, 10, 10, 10)
        .Add(fStatusView)
        .Add(fFileListView)
        .AddGroup(B_HORIZONTAL, 10)
        .Add(fAddButton)
        .Add(fRemoveButton)
        .AddGlue()
        .Add(fStartButton)
        .End()
        .Add(fExtractConfigBox);

    // 添加标签页
    fTabView->AddTab(fHomeView, new BTab());
    fTabView->TabAt(0)->SetLabel("主页");
    fTabView->AddTab(fCompressView, new BTab());
    fTabView->TabAt(1)->SetLabel("压缩");
    fTabView->AddTab(fExtractView, new BTab());
    fTabView->TabAt(2)->SetLabel("解压缩");

    // 设置标签页切换消息
    fTabView->SetTabWidth(B_WIDTH_FROM_WIDEST);
    fTabView->SetSelectionMessage(new BMessage(MSG_TAB_CHANGED));

    // 使用布局构建器组织界面
    BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
        .Add(menuBar)
        .AddGroup(B_VERTICAL, 10)
        .SetInsets(10, 10, 10, 10)
        .Add(fTabView)
        .End();
}

/**
 * @brief 创建菜单栏
 * @return 创建的菜单栏
 */
BMenuBar *MainWindow::_CreateMenuBar()
{
    BMenuBar *menuBar = new BMenuBar("menuBar");

    // 文件菜单
    BMenu *fileMenu = new BMenu("文件");
    fileMenu->AddItem(new BMenuItem("打开...", new BMessage(MSG_MENU_FILE_OPEN), 'O'));
    fileMenu->AddItem(new BMenuItem("保存...", new BMessage(MSG_MENU_FILE_SAVE), 'S'));
    fileMenu->AddSeparatorItem();
    fileMenu->AddItem(new BMenuItem("退出", new BMessage(MSG_MENU_FILE_QUIT), 'Q'));
    menuBar->AddItem(fileMenu);

    // 帮助菜单
    BMenu *helpMenu = new BMenu("帮助");
    helpMenu->AddItem(new BMenuItem("关于 HZip...", new BMessage(MSG_MENU_HELP_ABOUT)));
    menuBar->AddItem(helpMenu);

    return menuBar;
}

/**
 * @brief 执行压缩操作
 */
void MainWindow::_DoCompress()
{
    // 检查源路径是否有效
    if (fSourcePath.InitCheck() != B_OK)
    {
        BAlert *alert = new BAlert("错误", "请先选择要压缩的文件或目录", "确定");
        alert->Go();
        return;
    }

    // 如果目标路径未设置，打开保存文件面板
    if (fTargetPath.InitCheck() != B_OK)
    {
        fSavePanel->Show();
        return;
    }

    // 获取压缩配置
    BString compressionLevel;
    if (fSpeedRadioButton && fSpeedRadioButton->Value() == B_CONTROL_ON)
    {
        compressionLevel = "speed"; // 速度最快
    }
    else if (fSizeRadioButton && fSizeRadioButton->Value() == B_CONTROL_ON)
    {
        compressionLevel = "size"; // 体积最小
    }
    else
    {
        compressionLevel = "custom"; // 自定义
    }

    // 获取密码设置
    BString password;
    if (fPasswordCheckBox && fPasswordCheckBox->Value() == B_CONTROL_ON && fPasswordTextControl)
    {
        password = fPasswordTextControl->Text();
    }

    // TODO: 将来可以扩展HZipEngine，支持压缩级别和密码选项
    // 目前仅执行基本压缩操作
    bool result = fEngine.Compress(fCurrentFormat.String(), fSourcePath.Path(), fTargetPath.Path());

    // 显示结果
    if (result)
    {
        BString message("压缩成功: ");
        message.Append(fTargetPath.Path());
        BAlert *alert = new BAlert("成功", message.String(), "确定");
        alert->Go();
    }
    else
    {
        BAlert *alert = new BAlert("错误", "压缩操作失败", "确定");
        alert->Go();
    }

    // 重置路径
    fTargetPath.Unset();
}

/**
 * @brief 执行解压缩操作
 */
void MainWindow::_DoExtract()
{
    // 检查源路径是否有效
    if (fSourcePath.InitCheck() != B_OK)
    {
        BAlert *alert = new BAlert("错误", "请先选择要解压缩的文件", "确定");
        alert->Go();
        return;
    }

    // 显示解压配置界面，隐藏压缩配置界面
    if (fConfigBox)
        fConfigBox->Hide();
    if (fExtractConfigBox)
        fExtractConfigBox->Show();

    // 如果目标路径未设置，打开目录选择面板
    if (fTargetPath.InitCheck() != B_OK)
    {
        fExtractDirPanel->Show();
        return;
    }

    // 获取解压选项
    bool smartDelete = (fSmartDeleteCheckBox && fSmartDeleteCheckBox->Value() == B_CONTROL_ON);
    bool keepBroken = (fKeepBrokenCheckBox && fKeepBrokenCheckBox->Value() == B_CONTROL_ON);
    bool noSubdir = (fNoSubdirCheckBox && fNoSubdirCheckBox->Value() == B_CONTROL_ON);
    bool extractLater = (fExtractLaterCheckBox && fExtractLaterCheckBox->Value() == B_CONTROL_ON);

    // 执行解压缩操作
    bool result = fEngine.Extract(fSourcePath.Path(), fTargetPath.Path());

    // 显示结果
    if (result)
    {
        BString message("解压缩成功: ");
        message.Append(fTargetPath.Path());
        BAlert *alert = new BAlert("成功", message.String(), "确定");
        alert->Go();

        // 重置界面
        if (fConfigBox)
            fConfigBox->Show();
        if (fExtractConfigBox)
            fExtractConfigBox->Hide();
    }
    else
    {
        BAlert *alert = new BAlert("错误", "解压缩操作失败", "确定");
        alert->Go();
    }

    // 重置路径
    fTargetPath.Unset();
}

/**
 * @brief 显示关于对话框
 */
void MainWindow::_ShowAbout()
{
    BAlert *alert = new BAlert("关于 HZip",
                               "HZip 1.0\n\n"
                               "基于Haiku OS原生API开发的压缩/解压缩软件\n"
                               "支持多种常见压缩文件格式\n\n"
                               "© 2023 HZip 开发团队",
                               "确定");
    alert->Go();
}